Skip to content

ADDING mocking script for pesterV5 DOCS - #370

Closed
CN-CODEGOD wants to merge 4 commits into
pester:mainfrom
CN-CODEGOD:main
Closed

CN-CODEGOD wants to merge 4 commits into
pester:mainfrom
CN-CODEGOD:main

Conversation

@CN-CODEGOD

Copy link
Copy Markdown

i read about the issue

And test the function,it work fine in pesterV5 but fail in V4,it is a good assertion ,hoping to add the in scriptscope in modules future

### Mocking function in scriptblock
Unfortunately, we don't have a straightforward way to mock a function inside a scriptblock, but we could use some workarounds to accomplish this.

warning:Mocking may not work correctly in Pester versions below v5, so this function is only supported in Pester v5. In earlier versions, you cannot run the mock together with the script.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not feel right, I wrote about how to do this long time ago, and I don't think there is anything in Pester 5 that would make this work, it is simple mocking.

https://jakubjares.com/2019/06/09/testing-whole-scripts/ https://jakubjares.com/2019/06/14/testing-whole-scripts-part2/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any problem with the script tester ?
As far as i concern the inscript work well

   Add-Type -TypeDefinition @'
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Reflection;

namespace ScriptMocking
{
    [Cmdlet(VerbsData.Import, "Script")]
    public sealed class ImportScriptCommand : PSCmdlet
    {
        private static string Body = @"
            function ____placeholder {{ }}
            # alias to prevent main from running
            Set-Alias {0} '____placeholder'

            . {1} {2}
            # remove the alias, now we have all functions
            # from the script dot-sourced in this scope
            Remove-Item 'alias:{0}' -Force
            Remove-Item 'function:____placeholder' -Force";

        [Parameter(Position = 0, Mandatory = true)]
        public string Path { get; set; }

        [Parameter(Position = 1)]
        public string EntryPoint { get; set; }

        [Parameter]
        public string[] ArgumentList { get; set; }

        public ImportScriptCommand() {
            ArgumentList = new string[0];
            EntryPoint = "Main";
        }
        protected override void ProcessRecord()
        {
            var body =  string.Format(Body, EntryPoint, Path, string.Join(" ", ArgumentList));

            var sb = InvokeCommand.NewScriptBlock(body);

            var method = sb.GetType()
                .GetMethod("InvokeUsingCmdlet", BindingFlags.Instance | BindingFlags.NonPublic);

            var emptyArray = new object[0];
            var automationNull = new PSObject();
            const int writeToCurrentErrorPipe = 1;

            var @params = new object[]
                {this, false, writeToCurrentErrorPipe, automationNull, emptyArray, automationNull, new object[0]};

            method.Invoke(sb, @params);
        }
    }
}

@nohwnd

nohwnd commented Aug 11, 2026

Copy link
Copy Markdown
Member

Thanks for writing this up, but I can't take it as it is.

The code fence in your section is never closed, and the closing ``` at the end of the file is removed. Everything below your section renders as one code block and the rest of the page is broken.

The content is a copy of the workaround from pester/Pester#1069, including my -Name Jakub example, the O:\OneDrive\script-tester path and the pasted PS C:\Users\Administrator> prompt. That workaround reaches into PowerShell internals by reflection (InvokeUsingCmdlet via BindingFlags.NonPublic), I wrote it as a hack to answer that issue, and I don't want to document it as the recommended way to test a script.

The warning also uses a full width colon instead of a :::warning admonition, and it talks about versions below v5, which is not relevant in the v5 docs.

Closing this. The workaround stays in pester/Pester#1069 for anyone who needs it. If you want to document the general problem of testing a script that runs its own entry point, open an issue first and we can agree on the approach. Thanks!

🤖

@nohwnd nohwnd closed this Aug 11, 2026
@CN-CODEGOD

Copy link
Copy Markdown
Author

Thanks for writing this up, but I can't take it as it is.

The code fence in your section is never closed, and the closing ``` at the end of the file is removed. Everything below your section renders as one code block and the rest of the page is broken.

The content is a copy of the workaround from pester/Pester#1069, including my -Name Jakub example, the O:\OneDrive\script-tester path and the pasted PS C:\Users\Administrator> prompt. That workaround reaches into PowerShell internals by reflection (InvokeUsingCmdlet via BindingFlags.NonPublic), I wrote it as a hack to answer that issue, and I don't want to document it as the recommended way to test a script.

The warning also uses a full width colon instead of a :::warning admonition, and it talks about versions below v5, which is not relevant in the v5 docs.

Closing this. The workaround stays in pester/Pester#1069 for anyone who needs it. If you want to document the general problem of testing a script that runs its own entry point, open an issue first and we can agree on the approach. Thanks!

🤖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants